﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace TCPClient
{
    public partial class Form1 : Form
    {
        IPAddress address = IPAddress.Parse("127.0.0.1");
        int port = 8000;
        string ClientName = "Client";
        Socket client;
        HookManager _hookManager;

        public Form1()
        {
            InitializeComponent();
            _hookManager = new HookManager();
            _hookManager.Install();

            _hookManager.KeyDown += (sender, e) =>
            {
                lsSend.Items.Add(new ListViewItem(new string[] { "Nhấn Phím","", "" + e.KeyData.ToString().ToLower()}));
                if (!btnKetNoi.Visible)
                {
                    try
                    {
                        byte[] data = new byte[1024];
                        string send = "2| |" + e.KeyData.ToString().ToLower() + "| | ";
                        data = Encoding.ASCII.GetBytes(send);
                        client.Send(data, data.Length, SocketFlags.None);
                    }
                    catch (Exception ex)
                    {
                        txtKetNoi.Text += "Lỗi: " + ex;
                    }
                }           
            };
            _hookManager.MouseClick += (sender, e) =>
            {
                switch (e.Clicks)
                {
                    case 1:
                        lsSend.Items.Add(new ListViewItem(new string[] { "Nhấn Chuột", "Click Đơn", "" + e.Button, "" + e.X, "" + e.Y }));
                        break;
                    case 2:
                        lsSend.Items.RemoveAt(lsSend.Items.Count - 1);
                        lsSend.Items.Add(new ListViewItem(new string[] { "Nhấn Chuột", "Click Đôi", "" + e.Button, "" + e.X, "" + e.Y }));                                          
                        break;
                }
                
                if (!btnKetNoi.Visible && (e.Clicks==1 || e.Clicks==2))
                {
                    try
                    {
                        byte[] data = new byte[1024];
                        string send = "1|" + e.Clicks + "|" + e.Button + "|" + e.X + "|" + e.Y;
                        data = Encoding.ASCII.GetBytes(send);
                        client.Send(data, data.Length, SocketFlags.None);
                    }
                    catch (Exception ex)
                    {
                        txtKetNoi.Text += "Lỗi: " + ex;
                    }
                }                       
            }; 
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtIPServer.Text = address.ToString();
            txtPort.Text = port.ToString();
            txtClientName.Text = ClientName;
        }

        private void btnKetNoi_Click(object sender, EventArgs e)
        {
            ClientName = txtClientName.Text;
            try
            {
                IPEndPoint iep = new IPEndPoint(IPAddress.Parse(txtIPServer.Text), int.Parse(txtPort.Text));
                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                client.Connect(iep);
                byte[] data = new byte[1024];
                data = Encoding.ASCII.GetBytes(ClientName);
                client.Send(data, data.Length, SocketFlags.None);
                btnKetNoi.Visible = false;
                btnNgatKetNoi.Visible = true;
            }
            catch (SocketException)
            {
                txtKetNoi.Text += "Kết nối thất bại!" + Environment.NewLine;
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!btnKetNoi.Visible)
            {
                if (client.Connected)
                {
                    client.Disconnect(true);
                    client.Close();
                }
            }
        }

        private void btnNgatKetNoi_Click(object sender, EventArgs e)
        {
            client.Close();
            txtKetNoi.Text += "Đã ngắt kết nối!" + Environment.NewLine;
            btnKetNoi.Visible = true;
            btnNgatKetNoi.Visible = false;
        }
    }
}
